const cloud = require("@cloudbase/node-sdk");

const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV,
});

var db = app.database();
const _ = db.command
const ZXCode = db.collection('ZX-Code')

// 获取openId云函数入口函数
exports.main = async (event, context) => {

    // return await AION_ZB_WQ.get()
    var params = {}
    var type = event.data.type
    var code = event.data.code
    var isClose = event.data.isClose
    var _id = event.data._id
    var powerList = event.data.powerList

    if (type)
        if(type=='get'){
            return await ZXCode.where(params).get()
        }
        if(type=='add'&&code){
            params.code = code
            params.isClose = "1"
            params.powerList = []
            const res = await ZXCode.where({code:code}).get()
            if(res.data&&res.data.length>0){
                return res
            }else{
                return await ZXCode.add(params)
            }
        }
        if(type=='edit'){
            if(isClose)
                params.isClose = isClose
            if(powerList)
                params.powerList = powerList
            return await ZXCode.doc(_id).update(params)
        }

        if(type=='login'){
            if(code)
                params.code = code
            params.isClose = "1"
            return await ZXCode.where(params).get()
        }
};